/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mapper;
import dto.CustomerDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import utilities.ConnectDB;

/**
 *
 * @author Tam Nguyen
 */
public class CustomerMapper {
    
    public ArrayList<dto.CustomerDTO> LoadCustomer() throws SQLException
    {   
        ArrayList<dto.CustomerDTO> ds = new ArrayList<CustomerDTO>();
        try
        {

        String qry = "Select * from Customer as A,CustomerLogin as B where A.customer_id = B.customer_id  ";
        ResultSet rs = ConnectDB.getResultSet(qry);
        while(rs != null && rs.next())
            {
                dto.CustomerDTO cus = new CustomerDTO();
                cus.setCustomer_id(rs.getInt(1));
                cus.setFirst_name(rs.getString(2));
                cus.setLast_name(rs.getString(3));
                cus.setEmail(rs.getString(4));
                cus.setPhone(rs.getString(5));
                cus.setGender(rs.getString(6));
                cus.setAddress(rs.getString(7));
                cus.setBirthdate(rs.getString(8));
                cus.setRank(rs.getString(9));
                cus.setUsername(rs.getString(10));
                cus.setPass(rs.getString(11));
                
                ds.add(cus);
            }
        
        }
        catch(Exception e)
        {
        }
        return ds;
    }
    
    
    public boolean AddCustomer(CustomerDTO cus) throws SQLException
    {
        String qry = "Insert into Customer values ('"+cus.getCustomer_id()+"','"+cus.getFirst_name()+"','"+cus.getLast_name()+"','"+cus.getEmail()+"','"+cus.getPhone()+"','"+cus.getGender()+"','"+cus.getAddress()+"','"+cus.getBirthdate()+"', '"+cus.getRank()+"')";
        
        int i = ConnectDB.executeQueryString(qry);
        if(i == 1 )
        {
            return true;
        }
        return false;
        
    }

    public boolean AddUser(CustomerDTO cus) throws SQLException
    {
        String qry = "Insert into CustomerLogin values ('"+cus.getUsername()+"','"+cus.getPass()+"','"+cus.getCustomer_id()+"')";

        int i = ConnectDB.executeQueryString(qry);
        if(i == 1 )
        {
            return true;
        }
        return false;

    }
    
    public boolean DeleteCustomer(int cusid) throws SQLException
    {
        String qry = "delete from Customer where customer_id ="+cusid ;
        
        int i = ConnectDB.executeQueryString(qry);
        if(i == 1 )
        {
            return true;
        }
        return false;
        
    }
    public boolean DeleteUser(int cusid) throws SQLException
    {
        String qry = "delete from CustomerLogin where customer_id ="+cusid ;

        int i = ConnectDB.executeQueryString(qry);
        if(i == 1 )
        {
            return true;
        }
        return false;

    }


    
    public boolean UpdateCustomer(CustomerDTO cus) throws SQLException
    {
        String qry="Update Customer set first_name ='"+cus.getFirst_name()+"',last_name='"+cus.getLast_name()+"',email = '"+cus.getEmail()+"' ,phone = '"+cus.getPhone()+"',gender = '"+cus.getGender()+"', address = '"+cus.getAddress()+"', birthdate = '"+cus.getBirthdate()+"', rank = '"+cus.getRank()+"' where customer_id = "+cus.getCustomer_id();
        int flag = ConnectDB.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
        
    }

    public boolean UpdateUser(CustomerDTO cus) throws SQLException
    {
        String qry="Update CustomerLogin set username ='"+cus.getUsername()+"',pass='"+cus.getPass()+"' where customer_id = "+cus.getCustomer_id();
        int flag = ConnectDB.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
    
}
